﻿using System;
using System.Net;
using System.Windows;

namespace Transportr
{
    public class StationItemViewModel : BaseItemViewModel
    {
        private ServiceModel.StationLine rawData;

        public ServiceModel.StationLine RawData
        {
            get { return rawData; }
            set
            {
                rawData = value;
                NotifyPropertyChanged("stId");
                NotifyPropertyChanged("LineNumber");
                NotifyPropertyChanged("StationName");
                NotifyPropertyChanged("Company");
                NotifyPropertyChanged("EndStationName");
                NotifyPropertyChanged("LineTypeIconPath");
                NotifyPropertyChanged("Latitude");
                NotifyPropertyChanged("Longitude");
            }
        }

        public double Distance
        {
            get
            {
                if (LocationModel.Valid)
                    return Map.GeoPoint.CalculateDistance(LocationModel.CurrentLocation.Latitude, LocationModel.CurrentLocation.Longitude, Latitude, Longitude);
                else return 0;
            }
        }

        public string DistanceString
        {
            get
            {
                if (LocationModel.Valid)
                {
                    double dist = Distance;
                    if (dist < 1)
                        return String.Format("{0:0} m", dist * 1000);
                    else
                        return String.Format("{0:0.0} km", dist);
                }
                return "? m";
            }
        }

        public string Misc
        {
            get { return String.Format("{0},{1}", stId, rawData.slRoute); }
        }

        public int stId
        {
            get { return RawData.stId; }
        }

        public int lnId
        {
            get { return RawData.lnId; }
        }

        public string LineNumber
        {
            get { return RawData.lnNumber; }
        }

        public string StationName
        {
            get { return RawData.stName; }
        }
        public string Company
        {
            get { return RawData.coName; }
        }
        public string EndStationName
        {
            get { return RawData.EndStations.stName; }
        }
        public string LineTypeIconPath
        {
            get { return Icon.Vehicle((ServiceModel.VehicleType)RawData.ltId); }
        }
        public double Latitude
        {
            get { return RawData.stLat; }
        }
        public double Longitude
        {
            get { return RawData.stLng; }
        }

        public StationItemViewModel()
        {
            LocationModel.LocationChanged += () => { NotifyPropertyChanged("Distance"); };
        }
    }
}
